class ClientBattleHandle {
    static getInstance() {
        if (!ClientBattleHandle.instance) {
            ClientBattleHandle.instance = new ClientBattleHandle();
        }
        return ClientBattleHandle.instance;
    }
    init(p1, p2, cb, onEnd) {
        let fightAttriTeam1 = [],
            fightAttriTeam2 = [],
            battleQueue = [],
            team1 = {
                team: fightAttriTeam1
            },
            team2 = {
                team: fightAttriTeam2
            };
        p1.forEach((fightAttri, index) => {
            if (fightAttri) {
                fightAttri.teamId = 1;
                fightAttri.myTeam = team1;
                fightAttri.enemyTeam = team2;
                fightAttri.pos = index;
                fightAttriTeam1.push(fightAttri);
                battleQueue.push(fightAttri);
            }
        });
        team1.aliveNumber = fightAttriTeam1.length;
        p2.forEach((fightAttri, index) => {
            if (fightAttri) {
                fightAttri.teamId = 2;
                fightAttri.myTeam = team2;
                fightAttri.enemyTeam = team1;
                fightAttri.pos = index;
                fightAttriTeam2.push(fightAttri);
                battleQueue.push(fightAttri);
            }
        });
        team2.aliveNumber = fightAttriTeam2.length;

        let battleQueueLen = battleQueue.length;
        // let battleIndex = 0,
        //     round = 1,
        //     shield = 1,
        //     defenderIndex = 0;
        this.battleData = {
            team1: team1,
            team2: team2,
            battleQueue: battleQueue,
            battleQueueLen: battleQueue.length,
            battleIndex: 0,
            round: 1,
            shield: 1,
            defenderIndex: 0.
        }
        this.cb = cb;
        this.onEnd = onEnd;

    }

    getBattleInfo(animationCb) {
        let cb = this.cb,
            onEnd = this.onEnd;
        let battleData = this.battleData;
        let battleQueue = battleData.battleQueue,
            team1 = battleData.team1,
            team2 = battleData.team2;
        let attacker, defender, msg;
        let battleIndex = battleData.battleIndex,
            defenderIndex = battleData.defenderIndex;
        while (battleQueue[battleIndex].alive === false && battleIndex < battleData.battleQueueLen) {
            battleIndex += 1;
        }
        if (battleIndex < battleData.battleQueueLen) {
            attacker = battleQueue[battleIndex];
            let enemyTeam = attacker.enemyTeam.team;
            while (enemyTeam[defenderIndex].alive === false && defenderIndex < enemyTeam.length) {
                defenderIndex += 1;
            }
            if (defenderIndex < enemyTeam.length) {
                defender = enemyTeam[defenderIndex];
            }
        }
        animationCb(defenderIndex, attacker, defender);
    }
    attack(animationCb) {
        let cb = this.cb,
            onEnd = this.onEnd;
        let battleData = this.battleData;
        let battleQueue = battleData.battleQueue,
            team1 = battleData.team1,
            team2 = battleData.team2;
        let attacker, defender, msg;
        while (battleQueue[battleData.battleIndex].alive === false && battleData.battleIndex < battleData.battleQueueLen) {
            battleData.battleIndex += 1;
        }
        if (battleData.battleIndex < battleData.battleQueueLen) {
            attacker = battleQueue[battleData.battleIndex];
            let enemyTeam = attacker.enemyTeam.team;
            while (enemyTeam[battleData.defenderIndex].alive === false && battleData.defenderIndex < enemyTeam.length) {
                battleData.defenderIndex += 1;
            }
            if (battleData.defenderIndex < enemyTeam.length) {
                defender = enemyTeam[battleData.defenderIndex];
                let damage = this.calcDamage(attacker, defender);
                msg = `attacker:${attacker.name}|hp:${attacker.hp};defender:${defender.name}|hp:${defender.hp};伤害:${damage}`;
                // console.log(msg)
                let defenderIndex = battleData.defenderIndex;

                if (defender.hp <= 0) {
                    defender.alive = false;
                    attacker.enemyTeam.aliveNumber -= 1;
                    if (attacker.enemyTeam.aliveNumber === 0) {
                        onEnd();
                        return;
                    }
                }
                if (defender.currentShield > 0) {
                    let value = defender.currentShield - battleData.shield;
                    if (value < 0) {
                        defender.currentShield = 0;
                        battleData.shield = value * -1;
                        battleData.defenderIndex += 1;
                    } else {
                        battleData.shield = 0;
                        defender.currentShield = value;
                        battleData.defenderIndex = 0;
                        battleData.battleIndex += 1;
                        battleData.shield = 1
                    }
                } else {
                    battleData.defenderIndex += 1;
                }

                if (battleData.defenderIndex >= enemyTeam.length) {
                    battleData.defenderIndex = 0;
                    battleData.battleIndex += 1;
                    battleData.shield = 1
                }

                animationCb(defenderIndex, attacker, defender, damage, msg);
            } else {
                battleData.defenderIndex = 0;
                battleData.battleIndex += 1;
                battleData.shield = 1
            }
        }

        if (battleData.battleIndex >= battleData.battleQueueLen) {
            battleData.battleIndex = 0;
            battleData.defenderIndex = 0;
            battleData.shield = 1
            battleData.round += 1;
            if (battleData.round % 2 == 1) {
                battleQueue.forEach(item => {
                    if (item.alive !== false) {
                        item.currentShield = item.shield;
                        item.hp += item.recover;
                        if (item.hp > item.maxHp) {
                            item.hp = item.maxHp
                        }
                    }
                })
            }
            cb({
                round: battleData.round
            });
        }

    }
    //计算攻击
    calcDamage(attacker, defender) {
        let pdef = defender.pdef,
            attack = attacker.attack;
        let damage = Math.ceil(attack * attack / (pdef + attack)); //计算伤害
        defender.hp -= damage;

        return damage;
    }

}

let clientBattleHandle = ClientBattleHandle.getInstance();
export default clientBattleHandle;